import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const HGPTrouter = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../pages/home/index.vue'),
      children: [
        {
          path: '/reservationSet',
          name: 'reservationSet',
          component: () => import('../pages/reservationSet/index.vue')
        },
        {
          path: '/reservationSearch',
          name: 'reservationSearch',
          component: () => import('../pages/reservationSearch/index.vue')
        },
        {
          path: '/packageSet',
          name: 'packageSet',
          component: () => import('../pages/packageSet/index.vue')
        },
        {
          path: '/addMeasureData',
          name: 'addMeasureData',
          component: () => import('../pages/addMeasureData/index.vue')
        },
        {
          path: '/addNews',
          name: 'addNews',
          component: () => import('../pages/addNews/index.vue')
        },
      ]
    },
    {
      path: '/Login',
      name: 'login',
      component: () => import('../pages/login/index.vue')
    },
  ]
})
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
/* 
  登陆判断
*/
HGPTrouter.beforeEach((to, from, next) => {
  const isLogin = sessionStorage.getItem('token') ? true : false
  // 判断是否登陆
  if (isLogin) {
    next()
  } else {
    if (to.path !== "/login") {
      next({
        path: '/login',
        // query: {
        //   redirect: to.fullPath
        // }
      })
    } else {
      next()
    }
  }
})

export default HGPTrouter